﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmDangNhap : Form
    {
        public frmDangNhap()
        {
            InitializeComponent();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            TaiKhoanDTO entity = new TaiKhoanDTO();
            entity.Username = txtTenDangNhap.Text.Trim();
            entity.Password = GolbalSettings.Md5(txtMatKhau.Text.Trim());
            try
            {
                if (TaiKhoanBUS.Check(entity))
                {
                    if (entity.IsAdmin)
                    {
                        MessageBox.Show("Chào " + entity.Username + "!" + Environment.NewLine + Environment.NewLine + "Bạn có toàn quyền xem, thêm và chỉnh sửa dữ liệu.", "Da dang nhap", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);                        
                    }
                    GolbalSettings.CurrentUser = entity;
                    this.Close();
                }
                else
                {
                    DialogResult retry = MessageBox.Show("Sai mật khẩu hoặc tên đăng nhập.", "Access denied", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    if (retry == DialogResult.Retry)
                    {
                        txtMatKhau.Focus();
                        txtMatKhau.SelectAll();
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void txtTenDangNhap_TextChanged(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text != "" && txtMatKhau.Text != "")
                btnDongY.Enabled = true;
            else
                btnDongY.Enabled = false;
        }

        private void txtMatKhau_TextChanged(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text != "" && txtMatKhau.Text != "")
                btnDongY.Enabled = true;
            else
                btnDongY.Enabled = false;
        }

        private void frmDangNhap_Load(object sender, EventArgs e)
        {
            btnDongY.Enabled = false;
        }
    }
}
